package com.xinghou.platform.server.api.bussiness.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.xinghou.platform.util.*;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.main.MessageDAO;
import com.xinghou.platform.dao.main.PhotoDAO;
import com.xinghou.platform.dao.main.UserDAO;
import com.xinghou.platform.entity.resp.ShareInfoResp;
import com.xinghou.platform.model.main.Attachment;
import com.xinghou.platform.model.main.Message;
import com.xinghou.platform.model.main.Photo;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.model.main.UserInfo;
import com.xinghou.platform.server.api.bussiness.MessageBusinessService;

/**
 * @author wzk
 */
@Service("messageBusinessService")
public class MessageBuinessServiceImpl implements MessageBusinessService {
    private static String photominuri = ConfigUtil.getProperty("msg_file_download") + ConfigUtil.getProperty("MSGPHOTOMISSOURCEFODER");
    private static String photouri = ConfigUtil.getProperty("msg_file_download") + ConfigUtil.getProperty("MSGPHOTOSOURCEFODER");
    private static String headphotouri = ConfigUtil.getProperty("file_download");

    @Resource
    private UserDAO userDAO;
    @Resource
    private MessageDAO messageDAO;
    @Resource
    private PhotoDAO photoDAO;

    @Override
    public List<ShareInfoResp> hareInfoRespMessage(Integer userid, int state,
                                                   int startNum, int limitNum) throws DataAccessException {
        List<ShareInfoResp> infolist = new ArrayList<ShareInfoResp>();
        List<Message> messagelist = messageDAO.queryMsgGroupBySendId(userid, state, startNum, limitNum);

        for (Message msg : messagelist) {
            ShareInfoResp shareInfoResp = new ShareInfoResp();
            shareInfoResp.setUserid(msg.getSenderId() + "");
            shareInfoResp.setShareid(msg.getMsgId() + "");//借用shareid当作msgid

            User user = userDAO.getUserById(msg.getSenderId());
            int headid = user.getHeadid();

            shareInfoResp.setSex(user.getSex() + "");
            shareInfoResp.setNickname(user.getNickname());
            shareInfoResp.setAge(DateUtils.getAge(user.getBirthday()));
            shareInfoResp.setHoroscope(Gethoroscope.getConstellation(user.getBirthday()));
            shareInfoResp.setDate((msg.getCreateTime().toString()));
            shareInfoResp.setSharetype(String.valueOf(msg.getMsgType()));

            Integer attachmentId = msg.getAttachId();
            if (attachmentId != null) {
                if (headid != 0) {
                    Photo photo = photoDAO.getPhotoById(headid);
                    shareInfoResp.setSimphotourl(PhotoPathUtil.getDB160(headphotouri + photo.getPhotoUri()));
                    shareInfoResp.setPhotourl(headphotouri + photo.getPhotoUri());
                }
            }
            if (msg.getMsgType() == 1) {
                String text = msg.getText();
                String jumpnativepage = msg.getJumpNativePage();
                if (text != null) {
                    //shareInfoResp.setContent(StringUtils.substring(text, 0,17));
                    shareInfoResp.setContent(text);
                } else if (msg.getJumpUrl() != null) {
                    String jumpurl = photouri + msg.getJumpUrl();
                    shareInfoResp.setContent(jumpurl);
                } else if (jumpnativepage != null) {
                    shareInfoResp.setContent(jumpnativepage);
                }
            }

            shareInfoResp.setCountprivate(messageDAO.queryMsgCount(msg.getSenderId(), msg.getReceiverId(), msg.getOrigin(), state) + "");
            infolist.add(shareInfoResp);
        }
        return infolist;
    }

}
